(function() {
    'use strict';
    /**
     * @ngdoc function
     * @name webApp.controller:WmsCtrl
     * @description
     * # WmsCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('WmsCtrl', WmsCtrl);

    WmsCtrl.$inject = ['$scope', 'dataService', '$location', '$state', 'helper', '$timeout', '$rootScope', 'sku', 'MENU_Data', 'cacheService','ERP_CONFIG','sessionService','Upload'];

    function WmsCtrl($scope, dataService, $location, $state, helper, $timeout, $rootScope, sku, MENU_Data, cacheService,ERP_CONFIG,sessionService,Upload) {
        //显示
        $scope.listAction = function() {
            $scope.getList();
        }

        //获取列表
        $scope.getList = function(pageNo) {
            var query = {
                page_no: pageNo,
                page_size: $scope.pageSize,
                store_id: $rootScope.store_id,
            };
            dataService.wmsGet(query).then(function(list) {
                if (list) {
                    $scope.lists = list.list;
                    $scope.itemsTotal = list.count;
                }
            });
        }

        $scope.search =function(){
            $scope.getcustomelist(1);
        }

        //增加仓库
        $scope.wsmAdd = function() {
            if ($scope.wms) {
                $scope.wms.store_id = $rootScope.store_id;
                var data = clone($scope.wms);
                data.api = JSON.stringify(data.api);
                console.error("wmsAdd===", data);
                dataService.wmsAdd(data).then(function(list) {
                    if (!list.Msg) {
                        helper.myAlert("保存成功！", "#/wms/list");
                    } else {
                        helper.myAlert("保存失败！");
                    }
                });
            }
        }
        
        $scope.fileUpload = function(file, token) {
            if (!file) {
                helper.myAlert("请选择导入文件！");
                return false;
            }
            var filename = file.name.split('.');
            var hz= filename[filename.length-1];
            if(hz !=='xls'){
                helper.myAlert("导入模板文件不是xls");
                return false;
            }
            
            var token = sessionService.get('token');

            file.upload = Upload.upload({
                url: ERP_CONFIG.api2Url + '/api2/rest/',
                data: {
                    method: 'dhf.wms.hgitemimport',
                    file: file,
                    SHOP_CODE: $scope.shop_code,
                    store_id: $rootScope.store_id,
                    token: token
                }
            });
            // return false;
            file.upload.then(function(response) {
                $timeout(function() {
                    file.result = response.data;
                    if (!file.result.Msg) {
                        helper.myAlert("success");
                        $scope.customelistAction();
                    } else {
                        helper.myAlert("error" + file.result.Msg);
                    }
                });
            }, function(response) {}, function(evt) {});
        }

        //海关备案
        $scope.customelistAction = function() {
            $scope.pageSize = 10;
            $scope.shop_code = $location.search().shop_code;
            $scope.getcustomelist();
            $scope.mb = ERP_CONFIG.webUrl + '/web/dhfshop/app/images/item.xls';
        }
        
        $scope.getcustomelist = function(pageNo) {
            if(!$scope.shop_code){
                helper.myAlert("没有找到商家的海关编码！！", "#/wms/list");
            }
            $scope.pageNo = pageNo ? pageNo : "1";
            var query = {
                page_no: $scope.pageNo,
                page_size: $scope.pageSize,
                shop_code: $scope.shop_code
            }
            if($scope.itemKeyword){
                query.keyword=$scope.itemKeyword;
            }
            if($scope.SKU){
                query.SKU=$scope.SKU;
            }
            
            dataService.customsItemsGet(query).then(function(list) {
                if (list) {
                    $scope.customslist = list.list;
                    $scope.total_count = list.total_count;
                }
            });
        }

        

        //绑定id
        $scope.backpickpoint = function(da, wms) {
            if (da && wms) {
                wms.f_id = da;
                var data = {
                    wms_id: wms.wms_id,
                    store_id: $rootScope.store_id,
                    f_id: da
                }
                dataService.wmsBinding(data).then(function(list) {
                    if (!list.Msg) {
                        helper.myAlert("绑定成功！");
                        $scope.getList();
                    } else {
                        helper.myAlert("绑定失败！");
                    }
                });
            }
        }

        //删除
        $scope.del = function(id) {
            helper.myConfirm("确认删除仓库信息？",function(){
                if (id) {
                    var query = { "wms_id": id, "store_id": $rootScope.store_id };
                    dataService.wmsDel(query).then(function(list) {
                        if (list.Msg) {
                            helper.myAlert("error" + list.Msg);
                        } else {
                            helper.myAlert("成功！");
                            $scope.getList();

                        }
                    });
                }
            });
        }

        //添加修改操作
        $scope.addAction = function() {
            $scope.wms_id = $location.search().wms_id;
            if ($scope.wms_id) {
                var query = {
                    wms_id: $scope.wms_id,
                    store_id: $rootScope.store_id,
                };
                dataService.wmsGet(query).then(function(list) {
                    if (list && list.count>0) {
                        $scope.wms = list.list[0];
                        if ($scope.wms.api) {
                            $scope.wms.api = JSON.parse($scope.wms.api);
                        }
                        if($scope.wms.p_wms_id){
                            $scope.wms.p_wms_id = Number($scope.wms.p_wms_id);
                        }
                        
                    }else{
                        helper.myAlert("未找到数据",3,function(){
                            gourl("#/wms/list");
                        });
                    }
                });
            }
        }

        //添加修改操作
        $scope.customsitemsupdateAction = function() {
            $scope.sku = $location.search().sku;
            $scope.shop_code = $location.search().shop_code;
            if ($scope.sku) {
                var query = {
                    SKU: $scope.sku,
                    shop_code:$scope.shop_code
                };
                dataService.customsItemsGet(query).then(function(list) {
                    if (list) {
                        $scope.customsitems = list.list['0'];
                    }
                });
            }
        }

        //备案商品修改
        $scope.customsitemAdd = function() {
            $scope.shop_code = $location.search().shop_code;
            dataService.customsItemsUpdate($scope.customsitems).then(function(list) {
                if (!list.Msg) {
                    $scope.customsitemsupdateAction();
                    helper.message("保存成功！");
                } else {
                    helper.message("修改失败！","error");
                }
            });
        }

      
    }
})();